let staff_index_set = new Set()
let isFirstTime = 0;
let cancelMissionList = new Set()
let cancelMissionIndexList = new Set()

let getTimeCount = 0;
let firstTime;
let data = [
    {
        text: 'SUMMARY',

        children: [
            {
                order: 1,
                text: 'My Today Mission'
            },
            {
                order: 2,
                text: 'My Tomorrow Mission'
            }
        ]
    },
    {
        text: 'MISSION MANAGEMENT',
        children: [
            {
                order: 9,
                text: 'CAT OR CANCEL MISSION'
            }
        ]
    },
    {
        text: 'ACCOUNT',
        state: 'open',
        children: [
            {
                order: 7,
                text: 'ACCOUNT INFORMATION'
            },
            {
                order: 8,
                text: 'LOG OUT'
            }
        ]
    }
]

$('#sm').sidemenu({
    data: data,
    width: 240,
    multiple: false,
    onSelect: function (item) {
        switch (item.order) {
            case 1: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 2: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 3: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 4: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 5: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 6: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
            case 7: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }

            case 8: {
                $.post('/common/staffLogOut', null,
                    (res) => {
                        if (res.code == '449') {
                            alert(res.description)
                            document.location = "/public/index.html"
                        } else {
                            alert(res.description)
                            document.location = "/public/index.html"
                        }
                    }
                )

                break
            }
            case 9: {
                $('#tt').tabs('enableTab', item.text)
                $('#tt').tabs('select', item.text)
                break
            }
        }
    }
});

$('#tt').tabs(
    {
        onBeforeClose: function (title, index) {
            $('#tt').tabs(
                'disableTab', title
            )
            return false
        },
        onSelect: function (title, index) {
            if (title == 'My Today Mission') {
                document.getElementById("helpInformation").innerText = "select row for detail"
                $.post('/staff/catTodayMission', null, (res) => {
                    if (res.code == '449') {
                        alert(res.description)
                        document.location = "/public/index.html"
                    } else {

                        let todayMission = res.data;
                        $('#dg').datagrid({
                            data: todayMission,
                            fitColumns: true,
                            toolbar: '#tb',
                            striped: true,
                            singleSelect: true,
                            nowrap: false,
                            columns:
                                [[
                                    {
                                        field: 'mission_id', title: 'MISSION ID', width: '20%', align: 'center'
                                    }, {
                                        field: 'priority', title: 'PRIORITY', width: '20%', align: 'center'
                                    }, {
                                        field: 'mission_name', title: 'MISSION NAME', width: '20%', align: 'center'
                                    }, {
                                        field: 'deadline', title: 'DEADLINE', width: '20%', align: 'center'
                                    }, {
                                        field: 'status', title: 'STATUS', width: '20%', align: 'center'
                                    },
                                ]]
                        });

                    }
                    if (isFirstTime == 0) {
                        $('#tt').tabs('select', 3)
                        isFirstTime++;
                    }

                }, 'json')
            }
            if (title == 'My Tomorrow Mission') {
                document.getElementById("helpInformation").innerText = "select row for detail"
                $.post('/staff/catTomorrowMission', null, (res) => {
                    let tomorrowMission = res.data;
                    $('#dg2').datagrid({
                        data: tomorrowMission,
                        fitColumns: true,
                        striped: true,
                        toolbar: '#tb2',
                        singleSelect: true,
                        nowrap: false,
                        /*
                        *   <tr>
                        <th data-options="field:'mission_id',resizable:false" >MISSION ID</th>
                        <th data-options="field:'priority',resizable:false" >PRIORITY</th>
                        <th data-options="field:'mission_name',align:'right',resizable:false" width="15%">MISSION NAME</th>
                        <th data-options="field:'deadline',align:'right',resizable:false">DEADLINE</th>
                        <th data-options="field:'staff_id',resizable:false">STAFF ID</th>
                        <th data-options="field:'name',align:'center',resizable:false" width="15%">STAFF NAME</th>
                        <th data-options="field:'status',align:'center',resizable:false">STATUS</th>
                    </tr>*/
                        columns:
                            [[
                                {
                                    field: 'mission_id', title: 'MISSION ID', width: '20%', align: 'center'
                                }, {
                                    field: 'priority', title: 'PRIORITY', width: '20%', align: 'center'
                                }, {
                                    field: 'mission_name', title: 'MISSION NAME', width: '20%', align: 'center'
                                }, {
                                    field: 'deadline', title: 'DEADLINE', width: '20%', align: 'center'
                                }, {
                                    field: 'status', title: 'STATUS', width: '20%', align: 'center'
                                },
                            ]]
                    });


                }, 'json')
            }
            if (title == 'CAT OR CANCEL MISSION') {
                document.getElementById("helpInformation").innerText = "select missions which you want to cancel,then click 'CANCEL' button"

                $.post('/staff/catAllAssignMission', null, (res) => {
                    let AllStaff = res.data;
                    $('#dg4').datagrid({
                        data: AllStaff,
                        striped: true,
                        nowrap: false,
                        fitColumns: true,
                        toolbar: '#tb4',
                        singleSelect: false,
                        checkbox: true,
                        checkOnSelect: true,
                        selectOnCheck: true,
                        columns:
                            [[
                                /*
                                *       "mission_id": 38,
            "staff_id": 10246,
            "staff_name": "爱荷华·亚历山大亚1",
            "email": "1235@gmail.com",
            "content": "招聘吉他手一个,会弹小星星即可",
            "priority": "LEISURELY",
            "mission_name": "招聘吉他手一个",
            "deadline": "2020-06-21 09:28:21",
            "status": 0*/
                                {
                                    field: 'ck', title: 'CHOOSE',
                                    checkbox: true, width: '5%', align: 'center'
                                },
                                {
                                    field: 'mission_id', title: 'MISSION ID', width: '5%', align: 'center'
                                },
                                {
                                    field: 'mission_name', title: 'MISSION NAME', width: '20%', align: 'center'
                                }, {
                                    field: 'content', title: 'CONTENT', width: '20%', align: 'center'
                                },
                                {
                                    field: 'priority', title: 'PRIORITY', width: '20%', align: 'center'
                                },
                                {
                                    field: 'deadline', title: 'DEADLINE', sortable: true, width: '20%', align: 'center'
                                }, {
                                    field: 'status', title: 'STATUS', width: '10%', align: 'center'
                                },

                            ]]
                    });
                    for (const cancelMissionIndex of cancelMissionIndexList) {
                        $('#dg4').datagrid('selectRow', cancelMissionIndex)
                    }

                }, 'json')

            }

            if (title == 'MISSION LIST') {
                $.post('/admin/selectAllMission', null, (res) => {
                    let allMission = res.data;
                    $('#dg5').datagrid({
                        data: allMission,
                        striped: true,
                        nowrap: false,
                        toolbar: '#tb5',
                        singleSelect: false,
                        fitColumns: true,
                        checkbox: true,
                        checkOnSelect: true,
                        selectOnCheck: true,
                        columns:
                            [[{
                                field: 'ck', title: 'CHOOSE',
                                checkbox: true
                            },
                                {
                                    field: 'mission_id', title: 'MISSION ID', width: '8%', editor: {
                                        type: 'textbox'
                                        , options: {

                                            readonly: true
                                        }
                                    }
                                },
                                {
                                    field: 'mission_name', title: 'MISSION NAME', width: '18%', editor: {
                                        type: 'textbox'
                                    }
                                },
                                {
                                    field: 'content', title: 'MISSION CONTENT', width: '20%', editor: {
                                        type: 'textbox'
                                    }
                                }, {
                                    field: 'priority', title: 'PRIORITY', width: '15%', editor: {
                                        type: 'combobox',
                                        options: {
                                            valueField: 'priority',
                                            textField: 'priorityname',
                                            method: 'get',
                                            url: '/public/javascripts/priority.json',
                                            required: true
                                        }
                                    }
                                },
                                {
                                    field: 'deadline', title: 'DEADLINE', width: '20%', editor: {
                                        type: 'datetimebox', options: {

                                            formatter: function (date) {
                                                if (getTimeCount == 0) {

                                                    var y = date.getFullYear();
                                                    var m = date.getMonth() + 1;
                                                    var d = date.getDate();
                                                    var hh = date.getHours()
                                                    var mm = date.getMinutes()
                                                    var ss = date.getSeconds()
                                                    getTimeCount++;
                                                    firstTime = `${y}-${m}-${d} ${hh}:${mm}:${ss}`
                                                    return firstTime
                                                } else if (getTimeCount = 2) {

                                                    return firstTime
                                                } else {
                                                    getTimeCount++;
                                                    return firstTime
                                                }


                                            }
                                        }
                                    }
                                }, {
                                    field: 'type', title: 'TYPE', width: '15%', editor: {
                                        type: 'combobox',
                                        options: {
                                            valueField: 'type',
                                            textField: 'typename',
                                            method: 'get',
                                            url: '/public/javascripts/type.json',
                                            required: true
                                        }
                                    }
                                }
                            ]]
                    });
                    for (const staffSetElement of mission_index_set) {
                        $('#dg5').datagrid('selectRow', staffSetElement)
                    }
                }, 'json')


            }
            if (title == 'ACCOUNT INFORMATION') {
                $.post('/staff/catStaffAccount', null, (res) => {
                    document.getElementById("_easyui_textbox_input1").value = res[0].id
                    document.getElementById("_easyui_textbox_input2").value = res[0].name
                    document.getElementById("_easyui_textbox_input3").value = res[0].email
                    Cookies.set("staffemail",res[0].email)
                    Cookies.set("staffname",res[0].name)
                }, 'json')

            }
            if (title == 'ASSIGNED LIST') {
                $.post('/admin/selectAssignedMission', null, (res) => {
                    let assignedMission = res.data
                    $('#dg3').datagrid({
                        data: assignedMission,
                        striped: true,
                        nowrap: false,
                        fitColumns: true,
                        toolbar: '#tb3',
                        singleSelect: false,
                        checkbox: true,
                        checkOnSelect: true,
                        selectOnCheck: true,
                        /*
                        *   <tr>
                        <th data-options="field:'mission_id',resizable:false" >MISSION ID</th>
                        <th data-options="field:'priority',resizable:false" >PRIORITY</th>
                        <th data-options="field:'mission_name',align:'right',resizable:false" width="15%">MISSION NAME</th>
                        <th data-options="field:'deadline',align:'right',resizable:false">DEADLINE</th>
                        <th data-options="field:'staff_id',resizable:false">STAFF ID</th>
                        <th data-options="field:'name',align:'center',resizable:false" width="15%">STAFF NAME</th>
                        <th data-options="field:'status',align:'center',resizable:false">STATUS</th>
                    </tr>*/
                        columns:
                            [[{
                                field: 'ck', title: 'CHOOSE',
                                checkbox: true
                            },
                                /*  ,{"mission_id":13,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤菠萝回来",
                                "priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha",
                                "deadline":"2020-06-20 08:00:00","status":0}
                                ,{"mission_id":14,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤萝卜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha","deadline":"2020-06-20 08:00:00","status":0},{"mission_id":15,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤韭菜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha","deadline":"2020-06-20 08:00:00","status":0},{"mission_id":16,"staff_id":10246,
                                      "staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大",
                                      "content":"买5斤西瓜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha",
                                      "deadline":"2020-06-21 14:09:34","status":0
                          }*/
                                {
                                    field: 'mission_id', title: 'MISSION ID', width: '5%',
                                    align: 'center'
                                },
                                {
                                    field: 'mission_name', title: 'MISSION NAME', width: '20%',
                                    align: 'center'
                                },
                                {
                                    field: 'staff_id', title: 'STAFF ID', width: '15%',
                                    align: 'center'
                                },
                                {
                                    field: 'staff_name', title: 'STAFF NAME', width: '10%',
                                    align: 'center'
                                },
                                {
                                    field: 'priority', title: 'PRIORITY', align: 'center', width: '10%',
                                },
                                {
                                    field: 'deadline', title: 'DEADLINE', align: 'center', width: '15%',
                                },
                                {
                                    field: 'email', title: 'STAFF EMAIL', align: 'center', width: '15%',
                                },
                                {
                                    field: 'status', title: 'STATUS', align: 'center', width: '10%',
                                },
                            ]]
                    });
                }, 'json')

            }
            if (title == 'UNASSIGNED LIST') {
                $.post('/admin/selectUnAssignedMission', null, (res) => {
                    let unAssignedMission = res.data
                    $('#dg6').datagrid({
                        data: unAssignedMission,
                        striped: true,
                        nowrap: false,
                        singleSelect: false,
                        checkbox: true,
                        fitColumns: true,
                        checkOnSelect: true,
                        selectOnCheck: true,
                        /*
                        *   <tr>
                        <th data-options="field:'mission_id',resizable:false" >MISSION ID</th>
                        <th data-options="field:'priority',resizable:false" >PRIORITY</th>
                        <th data-options="field:'mission_name',align:'right',resizable:false" width="15%">MISSION NAME</th>
                        <th data-options="field:'deadline',align:'right',resizable:false">DEADLINE</th>
                        <th data-options="field:'staff_id',resizable:false">STAFF ID</th>
                        <th data-options="field:'name',align:'center',resizable:false" width="15%">STAFF NAME</th>
                        <th data-options="field:'status',align:'center',resizable:false">STATUS</th>
                    </tr>*/
                        columns:
                            [[
                                /*  ,{"mission_id":13,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤菠萝回来",
                                "priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha",
                                "deadline":"2020-06-20 08:00:00","status":0}
                                ,{"mission_id":14,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤萝卜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha","deadline":"2020-06-20 08:00:00","status":0},{"mission_id":15,"staff_id":10246,"staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大","content":"买5斤韭菜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha","deadline":"2020-06-20 08:00:00","status":0},{"mission_id":16,"staff_id":10246,
                                      "staff_name":"爱荷华·亚历山大亚大爱华·亚历山大爱荷华·亚历山大爱荷华·亚历山大爱荷华·亚历山大",
                                      "content":"买5斤西瓜回来","priority":"普通","mission_name":"买菠hahaha买萝haha买萝haha",
                                      "deadline":"2020-06-21 14:09:34","status":0
                          }*/
                                {
                                    field: 'mission_id', title: 'MISSION ID', width: '15%',
                                    align: 'center'
                                },
                                {
                                    field: 'mission_name', title: 'MISSION NAME', width: '25%',
                                    align: 'center'
                                },

                                {
                                    field: 'priority', title: 'PRIORITY', align: 'center', width: '25%',
                                },
                                {
                                    field: 'deadline', title: 'DEADLINE', align: 'center', width: '25%',
                                }

                            ]]
                    });
                }, 'json')

            }

        }
    }
)
$('#dg').datagrid({
    onSelect: function (rowIndex, rowData) {
        document.getElementById("detailInformation").innerText = rowData.content
    }
});


$('#dg2').datagrid({
    onSelect: function (rowIndex, rowData) {
        document.getElementById("detailInformation").innerText = rowData.content
    }
});


function submitForm() {
    let name = document.getElementById("_easyui_textbox_input2").value
    let email = document.getElementById("_easyui_textbox_input3").value
    $.post("/staff/modifyStaffAccount", {
        "name": name,
        "email": email
    }, (res) => {
        if (res.code == '449') {
            alert(res.description)
            document.location = "/public/index.html"
        } else {
            alert(res.description)
            document.location.reload()
        }
    })
}

$('input.easyui-validatebox').validatebox({
    validateOnCreate: false,
    err: function (target, message, action) {
        var opts = $(target).validatebox('options');
        message = message || opts.prompt;
        $.fn.validatebox.defaults.err(target, message, action);
    }
});
$('#dg4').datagrid({
    onSelect: function (rowIndex, rowData) {
        staff_index_set.add(rowIndex)
        staff_id_set.add(rowData.id)
    },
    onUnselect: function (rowIndex, rowData) {
        staff_index_set.delete(rowIndex)
        staff_id_set.delete(rowData.id)
    },
    onSelectAll: function (rows) {
        let length = rows.length
        for (let i = 0; i < length; i++) {
            staff_index_set.add(i)
            $('#dg4').datagrid('selectRow', i)
        }

        for (const row of rows) {
            staff_id_set.add(row.id)
        }
    },
    onUnselectAll: function (rows) {
        let length = rows.length
        for (let i = 0; i < length; i++) {
            staff_index_set.delete(i)
            $('#dg4').datagrid('unselectRow', i)
        }

        for (const row of rows) {
            staff_id_set.delete(row.id)
        }
    },
});
$('#dg3').datagrid({
    onSelect: function (rowIndex, rowData) {
        cancelMissionList.add(`${rowData.staff_id}+${rowData.mission_id}`)
    },
    onUnselect: function (rowIndex, rowData) {
        cancelMissionList.delete(`${rowData.staff_id}+${rowData.mission_id}`)
    },
    onSelectAll: function (rows) {
        for (const row of rows) {
            cancelMissionList.add(`${row.staff_id}+${row.mission_id}`)
        }
    },
    onUnselectAll: function (rows) {
        for (const row of rows) {
            cancelMissionList.delete(`${row.staff_id}+${row.mission_id}`)
        }
    }
});


$('#dg4').datagrid({
    onSelect: function (rowIndex, rowData) {
        cancelMissionIndexList.add(rowIndex)
        cancelMissionList.add(rowData.mission_id)
    },
    onUnselect: function (rowIndex, rowData) {
        cancelMissionIndexList.delete(rowIndex)
        cancelMissionList.delete(rowData.mission_id)
    },
    onSelectAll: function (rows) {
        let length = rows.length
        for (let i = 0; i < length; i++) {
            cancelMissionIndexList.add(rowIndex)
            $('#dg4').datagrid('selectRow', i)
        }

        for (const row of rows) {
            cancelMissionList.add(rowData.mission_id)
        }
    },
    onUnselectAll: function (rows) {
        let length = rows.length
        for (let i = 0; i < length; i++) {
            cancelMissionIndexList.delete(rowIndex)
            $('#dg4').datagrid('unselectRow', i)
        }

        for (const row of rows) {
            cancelMissionList.delete(rowData.mission_id)
        }
    }
});


let cancel = function () {
    for (const cancelMissionListElement of cancelMissionList) {
        $.post('/staff/cancelAMission',
            {
                'mission_id': cancelMissionListElement
            }, (res) => {
                if (res.code == '227') {
                    alert(`【mission:${cancelMissionListElement}】:${res.description}`)

                    document.location.reload()
                } else {
                    let span = document.createElement("span")
                    span.style.color = c
                    span.innerText = `********\n【mission:${cancelMissionListElement}】:【${res.description}】\n`
                    document.getElementById("detailInformation").appendChild(span)
                }

            }, 'json')
    }
}

let email = function () {
    $('#dlg').dialog('open')
}

let confirmEmail = function() {
    let row = $('#dg').datagrid('getSelected');
    if (row) {
        let remindTime = document.getElementById("dt").value;
        let email = Cookies.get("staffemail")
        let name = Cookies.get("staffname")
        $.post('/staff/emailRemind', {
            "remindTime":remindTime,
            "recipient":email,
            "content":`Don't forget finish mission: ${row.mission_id},${row.content}`
        },(res)=>{
            alert(res.description)
        },'json')
        /*
        * {"mission_id":38,"content":"招聘吉他手一个,会弹小星星即可","priority":"LEISURELY","mission_name":"招聘吉他手一个","deadline":"2020-06-21 09:28:21","status":0}*/
    } else {
        alert("you should choose a mission first")
    }


}

let finish = function(){
    let row = $('#dg').datagrid('getSelected');
    if (row) {
        $.post('/staff/finishMission', {
            "mission_id":row.mission_id
        },(res)=>{
            alert(res.description)
        },'json')
        /*
        * {"mission_id":38,"content":"招聘吉他手一个,会弹小星星即可","priority":"LEISURELY","mission_name":"招聘吉他手一个","deadline":"2020-06-21 09:28:21","status":0}*/
    } else {
        alert("you should choose a mission first")
    }

}
$.fn.datebox.defaults.formatter = function(date){
        var y = date.getFullYear();
        var m = date.getMonth() + 1;
        var d = date.getDate();

        return `${y}-${m}-${d}`

}

$.fn.datebox.defaults.parser=function (s) {
    var t = Date.parse(s);
    if (!isNaN(t)) {
        return new Date(t);
    } else {
        return new Date();
    }
}

window.addEventListener('load',()=>{
    document.getElementById("logout").innerText=Cookies.get("staffname")
   setInterval(()=>{
       document.getElementById("time").innerText=new Date().toLocaleString()
   },1000)
})
let fileuploaod = function () {
    let fileElement = document.getElementById("fileElement");
    let fileObject = fileElement.files[0]
    let formData = new FormData();
    formData.append("file", fileObject)
    $('#dlg2').dialog('open')
    $.ajax({
        url: '/staff/fileupload',
        type: 'POST',
        cache: false,    //上传文件不需要缓存
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头
        success: function (res) {
            if(!res.status.startsWith("4")){
                alert("Upload Sucess")
            }else {
                alert("Upload Failure")
            }
        }
    })
}